package io.renren.modules.substation.service.impl;

import io.renren.modules.substation.entity.TInfoAlarmConfigEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.substation.dao.TBaseDictInfoDao;
import io.renren.modules.substation.entity.TBaseDictInfoEntity;
import io.renren.modules.substation.service.TBaseDictInfoService;


@Service("tBaseDictInfoService")
public class TBaseDictInfoServiceImpl extends ServiceImpl<TBaseDictInfoDao, TBaseDictInfoEntity> implements TBaseDictInfoService {

    private static boolean cacheDictInfoFlag = false;
    private static Map<String, List<TBaseDictInfoEntity>> SysCacheDictInfoMap;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TBaseDictInfoEntity> page = this.page(
                new Query<TBaseDictInfoEntity>().getPage(params),
                new QueryWrapper<TBaseDictInfoEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public List<TBaseDictInfoEntity> queryByDictKey(String dictKey)
    {
        return baseMapper.queryByDictKey(dictKey);
    }

    /**
     * 获取全局缓存-字典信息
     */
    @Override
    public Map<String, List<TBaseDictInfoEntity>> getCacheDictInfoMap() {
        if (!cacheDictInfoFlag) {
            Map<String, List<TBaseDictInfoEntity>> map = new HashMap<>();
            List<TBaseDictInfoEntity> list = baseMapper.selectList(new QueryWrapper<TBaseDictInfoEntity>());
            List<TBaseDictInfoEntity> keyList;
            for (TBaseDictInfoEntity obj : list) {
                String key = obj.getDictKey();
                if (!map.containsKey(key))
                    keyList = new ArrayList<>();
                else
                    keyList = map.get(key);
                keyList.add(obj);
                map.put(key, keyList);
            }
            SysCacheDictInfoMap = map;
            cacheDictInfoFlag = true;
        }
        return SysCacheDictInfoMap;
    }

    @Override
    public TBaseDictInfoEntity getCacheDictInfoByKey(String dictKey) {
        TBaseDictInfoEntity info = null;
        if (!cacheDictInfoFlag)
            getCacheDictInfoMap();
        List<TBaseDictInfoEntity> list = SysCacheDictInfoMap.get(dictKey);
        if (list != null && list.size() > 0)
            return list.get(0);
        else
            return info;
    }

    /**
     * 获取指定key信息
     */
    @Override
    public List<TBaseDictInfoEntity> getCacheDictInfoList(String key) {
        if (!cacheDictInfoFlag)
            getCacheDictInfoMap();
        return SysCacheDictInfoMap.get(key);
    }

    @Override
    public Map<String, TBaseDictInfoEntity> getCacheDictInfoMapByKey(String key) {
        Map<String, TBaseDictInfoEntity> map = new HashMap<>();
        if (!cacheDictInfoFlag)
            getCacheDictInfoMap();
        List<TBaseDictInfoEntity> list = SysCacheDictInfoMap.get(key);
        for (TBaseDictInfoEntity o : list){
            map.put(o.getDictValue(), o);
        }
        return map;
    }


}